﻿using System.Collections.Generic;
using SuddenDeath.Domain.Services.FileManager.elFinder.Responses;
using SuddenDeath.Domain.Services.FileManager.elFinder.Utils;

namespace SuddenDeath.Domain.Services.FileManager.elFinder.Commands
{
    public class Paste : ICommand
    {
        public string Src { get; set; }

        public string Dst { get; set; }

        public IEnumerable<string> ToCopy { get; set; }

        public bool Cut { get; set; }

        public Paste(string src, string dst, string targets, string cut)
        {
            Src = src;

            Dst = dst;

            ToCopy = targets.Split(",".ToCharArray());

            Cut = !string.IsNullOrEmpty(cut) && cut == "1";

        }

        #region ICommand Members       

        public Response Execute()
        {
            var path = DirectoryUtils.GetFilePathByHash(Dst, Configuration.Configuration.RootPath);

            object error = null;

            object errData = null;

            foreach (var name in ToCopy)
            {
                var toCopy = DirectoryUtils.GetFilePathByHash(name, Configuration.Configuration.RootPath);

                try
                {
                    var fileInfo = new System.IO.FileInfo(toCopy);
                    if (Cut)
                    {
                        if (Src != Dst)
                            fileInfo.MoveTo(path + @"\" + fileInfo.Name);
                    }
                    else
                    {
                        if (Src != Dst)
                            fileInfo.CopyTo(path + @"\" + fileInfo.Name);
                        else
                            fileInfo.Duplicate();
                    }

                }
                catch
                {
                    var dirInfo = new System.IO.DirectoryInfo(toCopy);
                    if (Cut)
                    {
                        try
                        {
                            if (Src != Dst)
                                dirInfo.MoveTo(path + @"\" + dirInfo.Name);
                        }
                        catch
                        {
                            error = "Can't move directory";
                        }
                    }
                    else
                    {
                        try
                        {
                            if (Src != Dst)
                                dirInfo.CopyTo(path + @"\" + dirInfo.Name);
                            else
                                dirInfo.Duplicate();
                        }
                        catch
                        {
                            error = "Can't copy directory";                            
                        }
                    }
                }
            }

            var folder = new Folder(path);

            var response = new DirectoryResponse
                               {
                                   cwd = folder.Cwd,
                                   cdc = folder.Contents,
                                   tree = folder.Tree,
                                   Error = error,
                                   ErrorData = errData
                               };

            return response;
        }

        #endregion
    }
}
